﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Excellent
{
    public partial class AddForm : Form
    {
        public AddForm()
        {
            InitializeComponent();
        }

        private void buttadd_Click(object sender, EventArgs e)
        {
            string naem = this.txBname.Text.Trim();
            string subjects = this.comboBox1.SelectedValue.ToString();
            string results = this.tBresults.Text.Trim();
            DateTime Ptime = Convert.ToDateTime(this.dTPtime.Text.Trim());
            string sql = string.Format(@"insert into GradeTable(name,subjectsID,results,Thetesttime)" +
        "values('{0}', '{1}', '{2}', '{3}')", naem, subjects, results, Ptime);
            bool s = DBHelper.ExecuteNonQuery(sql);
            if (s)
            {
                MessageBox.Show("添加成功");
            }
            else { MessageBox.Show("添加失败"); }
        }

        private void DGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
          

        }

        private void AddForm_Load(object sender, EventArgs e)
        {
            string sql = string.Format(@"select * from GLAcct  ");
            DataTable tb = DBHelper.GetDataSet(sql).Tables[0];
            DataRow dr = tb.Rows[0];
            comboBox1.ValueMember = "subjectsID";
            comboBox1.DisplayMember = "subjects";
            comboBox1.DataSource = tb;

            string sql1 = string.Format(@"select ID,name,results,subjects,Thetesttime from GradeTable a, GLAcct b where  a.subjectsID = b.subjectsID");
            DataSet ds = DBHelper.GetDataSet(sql1);
            DataTable dt = ds.Tables[0];
            if (dt.Rows.Count > 0)
            {
                this.DGV.DataSource = dt;
            }
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string sql = string.Format(@"select ID,name,results,subjects,Thetesttime from GradeTable a, GLAcct b where  a.subjectsID = b.subjectsID");
            DataSet ds = DBHelper.GetDataSet(sql);
            DataTable dt = ds.Tables[0];
            if (dt.Rows.Count > 0)
            {
                this.DGV.DataSource = dt;
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int UserID = Convert.ToInt32(this.DGV.CurrentRow.Cells[0].Value.ToString());
            string sql = string.Format("delete from GradeTable where ID='{0}'", UserID);
            bool row = DBHelper.ExecuteNonQuery(sql);
            if (row )
            {
                MessageBox.Show("删除成功");
            }
            else
            {
                MessageBox.Show("删除失败");
            }
        }
    }
}
